<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;

class AdminController extends Controller
{
    public function index()
    {
        /*-------------------------天气接口------------------------------*/
        // var_dump($_SERVER["REMOTE_ADDR"]);
        $host = "http://ali-weather.showapi.com";
        $path = "/ip-to-weather";
        $method = "GET";
        $appcode = "8419895ebf3b45f4a5aace5979dc8f1b";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "ip=121.69.23.250&need3HourForcast=0&needAlarm=0&needHourData=0&needIndex=0&needMoreDay=0";
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $con=curl_exec($curl);
        $data=json_decode($con,true);
        /*-------------------------天气接口end------------------------------*/
        /*---------------------------首页数据-------------------------------*/
            //获取当前时间戳
            $nowtime=time();
            //获取一周内注册用户
            $t=$nowtime-604800;
            //按时间输出此时间段内的用户
            $users=DB::table('user')->where('time','>',$t)->count();

            //获取一周内发布的商品个数
            $gnum=DB::table('goods')->where('time','>',$t)->count();
            //获取一周内发布文章个数
            $numart=DB::table('article')->where('time','>',$t)->count();
            //获取一周内评论文章个数
            $nucom=DB::table('blogreply')->where('time','>',$t)->count();

        /*---------------------------首页数据end----------------------------*/
        $admin=DB::table('user')->where('id',session('uid'))->first();
        $messages=DB::table('admin_message')->where('status','1')->get();
        $num=DB::table('admin_message')->where('status','1')->count();
        session(['messages'=>$messages]);
        session(['num'=>$num]);
        session(['admin'=>$admin]);

        return view('admin.index',['day'=>$data['showapi_res_body']['f1'],'users'=>$users,'numart'=>$numart,'nucom'=>$nucom,'gnum'=>$gnum]);

    }
    public function login()
    {
    	return view('admin.login');
    }
    public function dologin(Request $request)
    {
    	
    	$this->validate($request,[
            'username'=>'required|regex:/^\w{5}$/',
            ],[
            'username.required'=>'用户名不能为空',
            'username.regex'=>'用户名格式不正确',
            ]);
        $data=$request->except(['_token']);
        $admin=DB::table('user')->where('username',$data['username'])->first();
        if(!$admin){
            return back()->with('error','用户名不存在');
        }

        $res=Hash::check($data['password'],$admin->password);

        if($res){
           session(['uid'=>$admin->id]);
            return redirect('/admin')->with('info','登录成功');
        }else{
            return back()->with('error','密码错误');
        }    
    }
    public function logout(Request $request)
    {
        $request->session()->forget('uid');
        return redirect('/admin');
    }

}
